package org.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.demo.entity.Users;
import org.demo.mapper.UsersMapper;
import org.demo.service.IOrderIdService;
import org.demo.service.IUsersService;
import org.demo.utils.Md5Util;
import org.demo.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {
    @Autowired
    private UsersMapper userMapper;
    @Autowired
    private IOrderIdService orderIdService;

    @Override
    public Users findByUserName(String username) {
        Users u = userMapper.findByUserName(username);
        return u;
    }

    @Override
    @Transactional
    public void register(Users user) {
        String id = orderIdService.getId("UNID");
        user.setId(id);
        //加密 密码
        String password = Md5Util.getMD5String(user.getPassword());
        user.setUsername(user.getUsername());
        user.setPassword(password);
        user.setRoleId(user.getRoleId());
        userMapper.add(user);

    }

    @Override
    public void updateUser(Users user) {
        user.setUpdateTime(new Date());
        userMapper.updateById(user);
//        userMapper.updateUser(user);
    }
    @Override
    public void addUser(Users user) {
        String id = orderIdService.getId("UNID");
        user.setId(id);
        if (user.getPassword().equals("") || user.getPassword()==null){
            String password = Md5Util.getMD5String("123456");
            user.setUsername(user.getUsername());
            user.setPassword(password);
        }

        user.setCreateTime(new Date());
        save(user);
    }
    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        String id = (String) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd) {
        //token获取id
        Map<String,Object> map = ThreadLocalUtil.get();
        String id = (String) map.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
    }



    @Override
    public List<Users> listAll(Users users) {
        List<Users> Users = userMapper.listAll(users);
        return Users;
    }
    @Override
    public List<Users> listLan(Users users) {
        List<Users> Lan = lambdaQuery().eq(Users::getRoleId,"ROLE_landlord").list();
        return Lan;
    }

    @Override
    public List<Users> listInt(Users users) {
        List<Users> Intlist = lambdaQuery().eq(Users::getRoleId,"ROLE_intermediary").list();
        return Intlist;
    }

    @Override
    public List<Users> listCus(Users users) {
        List<Users> Cuslist = lambdaQuery().eq(Users::getRoleId,"ROLE_customer").list();
        return Cuslist;
    }
}
